package drongo.desktop.utils
{
	import flash.display.BitmapData;
	import flash.display.IBitmapDrawable;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;

	public class BitmapDataUtil{
		
		public function BitmapDataUtil(){
			
		}
		private static const HELP_RECT:Rectangle=new Rectangle();
		private static const HELP_MATRIX:Matrix=new Matrix();
		private static const HELP_POINT:Point=new Point();
		/**
		 *  
		 * @param target						要绘制到哪个目标上
		 * @param source						数据源
		 * @param rect							源内容矩形
		 * @param pt							绘制到目标上的起始点
		 * @param colorTrensform
		 * @param scale
		 * 
		 */		
		public static function draw(target:BitmapData,source:IBitmapDrawable,rect:Rectangle,point:Point,colorTrensform:ColorTransform=null,scale:Number=1):void{
			HELP_RECT.x=point.x;
			HELP_RECT.y=point.y;
			HELP_RECT.width=rect.width*scale;
			HELP_RECT.height=rect.height*scale;
			HELP_MATRIX.tx=point.x-rect.x;
			HELP_MATRIX.ty=point.y-rect.y;
			HELP_MATRIX.a=HELP_MATRIX.d=scale;
			target.draw(source,HELP_MATRIX,colorTrensform,null,HELP_RECT,false);
		}
		/**
		 * 切图 
		 * @param source	原图
		 * @param size		切片大小
		 * @return 			返回一个以 索引 x_y 为key的字典
		 */		
		public static function cut(source:BitmapData,size:uint):Dictionary{
			var colorList:Vector.<uint>=source.getVector(source.rect);
			var map:Dictionary=new Dictionary();
			var r:uint=source.width/size+0.5;
			var c:uint=source.height/size+0.5;
			var block:BitmapData;
			var cutRect:Rectangle=new Rectangle();
			HELP_POINT.x=HELP_POINT.y=0;
			for (var y:int = 0; y < c; y++) 
			{
				for (var x:int = 0; x < r; x++) 
				{
					cutRect.x=x*size;
					cutRect.y=y*size;
					cutRect.width=size;
					cutRect.height=size;
					block=new BitmapData(size,size);
					block.copyPixels(source,cutRect,HELP_POINT);
					map[x+"_"+y]=block;
				}
			}
			return map;
		}
		/**
		 * 获取位图透明通道数据集合
		 * @param bitmapData
		 * @param bitmapSampling
		 * @return
		 */
		public static function getAlphaData(bitmapData:BitmapData,bitmapSampling:Number=1):Vector.<uint>{
			var w:Number=bitmapData.width;
			var h:Number=bitmapData.height;
			if(bitmapSampling>1||bitmapSampling<.1) throw new Error("Incorrect bitmap sampling, correct range is 0.1 - 1");
			
			var tempBitmapData:BitmapData = new BitmapData ( Math.ceil ( w * bitmapSampling) , Math.ceil ( h * bitmapSampling ), true, 0x00000000 );
			if(bitmapSampling < 1){
				tempBitmapData.draw (bitmapData, new Matrix ( bitmapSampling, 0, 0, bitmapSampling, 0, 0) ) 
			}else{
				tempBitmapData = bitmapData.clone();
			}
			var tempData:Vector.<uint>=tempBitmapData.getVector(tempBitmapData.rect);
			var alphaData:Vector.<uint>=new Vector.<uint>(Math.ceil((w*h)/4),true);
			
			var j:uint=0,len:uint=alphaData.length;
			for (var i:int = 0; i < len; i++) 
			{
				alphaData[i]= ( ColorUtil.getAlpha(tempData[j])<<24 ) | ( ColorUtil.getAlpha(tempData[j+1])<<16) | ( ColorUtil.getAlpha(tempData[j+2])<<8) |  ( ColorUtil.getAlpha(tempData[j+3] ));
				j+=4;
			}
			tempData=null;
			tempBitmapData.dispose();
			tempBitmapData=null;
			
			return alphaData;
		}
	}
}